home *** CD-ROM | disk | FTP | other *** search
- /*
- File: marquee.c
-
- Contains: This sample will demonstrate an implementation of an "animated" selection box.
-
- Written by: JB
-
- Copyright: Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 08/2000 JM Carbonized, non-Carbon code is commented out
- for demonstration purposes.
- 7/9/1999 KG Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
- #include "CarbonPrefix.h"
- #include <QuickDraw.h>
- #include <Events.h>
- #include <Fonts.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <Packages.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
-
- void Resume();
- void TrackMarquee(Point start,Rect* resultRect);
- void SetMobiusRect(Rect* rect,short left,short top, short right,short bottom);
-
- void Resume() {
- ExitToShell();
- }
-
- void main() {
- EventRecord event;
- //WindowRecord window;
- WindowPtr window;
- Rect bounds, rect;
- Point start;
-
- /*
- ** initialize the macintosh
- */
- /*InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);*/
- InitCursor();
-
- SetRect(&bounds, 50, 50, 500, 300);
- //NewWindow((Ptr)&window, &bounds, "\pApple Computer Inc.", true, noGrowDocProc, (WindowPtr)-1L, false, 0L);
- //SetPort((GrafPtr)&window);
- window = NewCWindow(nil, &bounds, "\pApple Computer Inc.", true, noGrowDocProc, (WindowPtr) -1, false, 0L);
- SetPortWindowPort(window);
-
-
- SetRect(&rect, 0, 0, 0, 0);
- while (true) {
- GetNextEvent(everyEvent, &event);
- switch (event.what) {
- case mouseDown :
- start = event.where;
- GlobalToLocal(&start);
- TrackMarquee(start, &rect);
- break;
- case keyDown :
- ExitToShell();
- break;
- }
- }
- }
-
- /*
- ** Description
- ** TrackMarquee will display a marquee similar to the
- ** selection rectangle tool in MacPaint™. It is assumed that the
- ** current port has been set before calling.
- **
- ** Parameters
- ** start : the local coordinates where the mouse down occured.
- ** resultRect : the final rectangle that was selected.
- */
-
- #define TICKDELAY 2
-
- void TrackMarquee(Point start,Rect* resultRect)
- {
- /*
- ** there are fifteen patterns defined here
- ** each one eight bytes long starting at :
- ** patterns[0], patterns[1], patterns[2], patterns[3],
- ** patterns[4], patterns[5], patterns[6], patterns[7]
- */
- static unsigned char patterns[] = {
- 0xF8, 0xF1, 0xE3, 0xC7, 0x8F,
- 0x1F, 0x3E, 0x7C, 0xF8, 0xF1,
- 0xE3, 0xC7, 0x8F, 0x1F, 0x3E
- };
-
- auto Point mouse; /* the current mouse location */
- register short index; /* the index of the current patterns array */
- auto Rect nowRect, /* the current rectangle to be framed */
- thenRect; /* the last rectangle to be framed */
- auto long nowTicks, /* the current tick count */
- thenTicks; /* the last tick count */
- auto PenState penState; /* the saved pen state on entry to procedure */
- GrafPtr port;
- RgnHandle rgnHandle = NewRgn();
-
- GetPort(&port);
-
-
- thenTicks = 0;
- index = 0;
-
- GetPenState(&penState);
- PenMode(patXor);
-
- PenPat((Pattern*)&patterns[index]);
- SetRect(&nowRect, start.h, start.v, start.h, start.v);
- FrameRect(&nowRect);
- thenRect = nowRect;
-
- while (StillDown()) {
- nowTicks = TickCount();
- GetMouse(&mouse);
- SetMobiusRect(&nowRect, start.h, start.v, mouse.h, mouse.v);
- if (((thenTicks + TICKDELAY) < nowTicks ? thenTicks = nowTicks, true : false) ||
- (!EqualRect(&nowRect, &thenRect))) {
- FrameRect(&thenRect);
- index = index < 7 ? index + 1 : 0;
- PenPat((Pattern*)&patterns[index]);
- FrameRect(&nowRect);
- thenRect = nowRect;
- }
- QDFlushPortBuffer(port, GetPortVisibleRegion(port, rgnHandle));
- }
- FrameRect(&thenRect);
-
- SetPenState(&penState);
- *resultRect = thenRect;
- DisposeRgn(rgnHandle);
- }
-
- void SetMobiusRect(Rect* rect,short left,short top, short right,short bottom)
- {
- if (left > right) {
- rect->left = right;
- rect->right = left;
- } else {
- rect->left = left;
- rect->right = right;
- }
- if (top > bottom) {
- rect->top = bottom;
- rect->bottom = top;
- } else {
- rect->top = top;
- rect->bottom = bottom;
- }
- }
-